package com.ycdx.impms.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.ycdx.impms.impl.RyxxImpl;
import com.ycdx.impms.pojo.Ryxx;

public class RyxxAction extends ActionSupport implements ModelDriven<Ryxx>{
	
	private static final long serialVersionUID = -8165556436385048107L;
	
	boolean success ;	
	String message;
	
	private Ryxx ryxx = new Ryxx();
	
	private JSONObject result;//返回的json	
    private String rows;//每页显示的记录数        
    private String page;//当前第几页    
    private String area;
    private String act;
    private String queryName;
    
    ActionContext ctx = ActionContext.getContext(); 
    HttpServletRequest request = (HttpServletRequest)ctx.get(ServletActionContext.HTTP_REQUEST);       
    HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE); 
    
	public JSONObject getResult() {
		return result;
	}

	public void setResult(JSONObject result) {
		this.result = result;
	}

	public String getRows() {
		return rows;
	}

	public void setRows(String rows) {
		this.rows = rows;
	}

	public String getPage() {
		return page;
	}

	public void setPage(String page) {
		this.page = page;
	}
    
	
	
	@Override
	public String execute() throws Exception {
		//System.out.println(action);
		if(act.equals("list")){
			return list();
		}else if(act.equals("query")){
			return query();
		}else{
			return SUCCESS;
		}
	}
	
	public String query(){
		//System.out.println(area);
		//当前页 
        int intPage = Integer.parseInt((page == null || page == "0") ? "1":page); 
        //每页显示条数 
        int number = Integer.parseInt((rows == null || rows == "0") ? "20":rows); 
        //每页的开始记录  第一页为1  第二页为number +1  
        int start = (intPage-1)*number; 
           
        List<Ryxx> list = RyxxImpl.queryByName(start,number,area, queryName);//每页的数据，放入list 
        Map<String, Object> jsonMap = new HashMap<String, Object>();//定义map 
        jsonMap.put("total", RyxxImpl.getQueryResultCount(area, queryName));//total键 存放总记录数，必须的 
        jsonMap.put("rows", list);//rows键 存放每页记录 list 
        result = JSONObject.fromObject(jsonMap);//格式化result   一定要是JSONObject 
        //System.out.println(result);   
        //result = JSONArray.fromObject(jsonMap);   
        return SUCCESS; 
	}
	
	/**
	 * 列表所有人员信息
	 * @return
	 */
	public String list(){
		//System.out.println(area);
		//当前页 
        int intPage = Integer.parseInt((page == null || page == "0") ? "1":page); 
        //每页显示条数 
        int number = Integer.parseInt((rows == null || rows == "0") ? "20":rows); 
        //每页的开始记录  第一页为1  第二页为number +1  
        int start = (intPage-1)*number; 
           
        List<Ryxx> list = RyxxImpl.getRyxxList(start, number, area);//每页的数据，放入list 
        Map<String, Object> jsonMap = new HashMap<String, Object>();//定义map 
        jsonMap.put("total", RyxxImpl.getCount(area));//total键 存放总记录数，必须的 
        jsonMap.put("rows", list);//rows键 存放每页记录 list 
        result = JSONObject.fromObject(jsonMap);//格式化result   一定要是JSONObject 
        //System.out.println(result);   
        //result = JSONArray.fromObject(jsonMap);   
        return SUCCESS; 

	}

	public String getArea() {
		return area;
	}

	public void setArea(String area) {
		this.area = area;
	}

	public Ryxx getModel() {
		// TODO Auto-generated method stub
		return ryxx;
	}
    
	/**
	 * 添加人员信息
	 * @return
	 */
	public String add() {
		int flag = RyxxImpl.addRyxx(ryxx);
		if(flag==1){
			//添加成功
			success = true;
			message = "人员信息添加成功！";
		}else{
			//添加不成功
			success = false;
			message = "人员信息添加失败，请检查！";
		}
		Map<String, Object> jsonMap = new HashMap<String, Object>();//定义map 
		jsonMap.put("success", success);
        jsonMap.put("message", message);
        result = JSONObject.fromObject(jsonMap);//格式化result   一定要是JSONObject 
        // System.out.println(result);
        return SUCCESS;
	}
	/**
	 * 编辑人员信息
	 * @return
	 */
	public String edit(){
		//System.out.println(ryxx.getId());
		//System.out.println(ryxx.getDw());
		int flag = RyxxImpl.updateRyxx(ryxx.getId(), ryxx);
		if(flag==1){
			//添加成功
			success = true;
			message = "人员信息编辑成功！";
		}else{
			//添加不成功
			success = false;
			message = "人员信息编辑失败，请检查！";
		}
		Map<String, Object> jsonMap = new HashMap<String, Object>();//定义map 
		jsonMap.put("success", success);
        jsonMap.put("message", message);
        result = JSONObject.fromObject(jsonMap);//格式化result   一定要是JSONObject 
        //System.out.println(result);
        return SUCCESS;
	}
	
	public String remove(){
		int flag = RyxxImpl.removeRyxx(ryxx.getId());
		if(flag==1){
			//添加成功
			success = true;
			message = "编号为"+ryxx.getId()+"的人员信息删除成功！";
		}else{
			//添加不成功
			success = false;
			message = "编号为"+ryxx.getId()+"的人员信息删除失败，请检查！";
		}
		Map<String, Object> jsonMap = new HashMap<String, Object>();//定义map 
		jsonMap.put("success", success);
        jsonMap.put("message", message);
        result = JSONObject.fromObject(jsonMap);//格式化result   一定要是JSONObject 
        //System.out.println(result);
        return SUCCESS;
	}


	public String getAct() {
		return act;
	}

	public void setAct(String act) {
		this.act = act;
	}

	public String getQueryName() {
		return queryName;
	}

	public void setQueryName(String queryName) {
		this.queryName = queryName;
	}
}
